home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_dev.lha / GMSDev / Source / C / 3DObjects / Enneper.c next >
Encoding:
C/C++ Source or Header  |  1998-08-23  |  4.3 KB  |  165 lines

  1. /* SAS/C: sc Enneper.c opt math=standard INCDIR=INCLUDES:
  2. **
  3. ** This is a demo of a rotating Enneper's Surface.  The object is
  4. ** pre-calculated then rotated in real time for speed, although things could
  5. ** be faster than this.
  6. **
  7. ** Improve: Replace doubles with longs/words if possible.
  8. */
  9.  
  10. #include <proto/dpkernel.h>
  11. #include <math.h>
  12.  
  13. BYTE *ProgName      = "Enneper's Surface";
  14. BYTE *ProgAuthor    = "Paul Manias";
  15. BYTE *ProgDate      = "January 1998";
  16. BYTE *ProgCopyright = "DreamWorld Productions (c) 1996-1998.  Freely distributable.";
  17. BYTE *ProgShort     = "3-Dimensional object demonstration.";
  18.  
  19. struct GScreen *screen;
  20. struct JoyData *jport1;
  21.  
  22. void Demo(void);
  23.  
  24. struct DotPixel { double X,Y,Z; };
  25.  
  26. #define AMTCOLOURS 32
  27.  
  28. LONG palette[AMTCOLOURS+2] = {
  29.   PALETTE_ARRAY,32,
  30.   0x000000L,0x101010L,0x171717L,0x202020L,0x272727L,0x303030L,0x373737L,0x404040L,
  31.   0x474747L,0x505050L,0x575757L,0x606060L,0x676767L,0x707070L,0x777777L,0x808080L,
  32.   0x878787L,0x909090L,0x979797L,0xa0a0a0L,0xa7a7a7L,0xb0b0b0L,0xb7b7b7L,0xc0c0c0L,
  33.   0xc7c7c7L,0xd0d0d0L,0xd7d7d7L,0xe0e0e0L,0xe0e0e0L,0xf0f0f0L,0xf7f7f7L,0xffffffL
  34. };
  35.  
  36. /***********************************************************************************/
  37.  
  38. void main(void)
  39. {
  40.   if (screen = InitTags(NULL,
  41.      TAGS_SCREEN,    NULL,
  42.      GSA_Attrib,     SCR_DBLBUFFER,
  43.        GSA_BitmapTags, NULL,
  44.        BMA_Palette,    palette,
  45.        TAGEND, NULL,
  46.      TAGEND)) {
  47.  
  48.      if (jport1 = Init(Get(ID_JOYDATA),NULL)) {
  49.         Display(screen);
  50.         Demo();
  51.  
  52.      Free(jport1);
  53.      }
  54.   Free(screen);
  55.   }
  56. }
  57.  
  58. /************************************************************************************
  59. ** Longtitude deterimines the position of the dot on the horizontal axis.
  60. ** Latitude determines the position of the dot on the vertical axis.
  61. */
  62.  
  63. #define AMTDOTS 1000       /* The amount of dots in the Object. */
  64.  
  65. #define MAXZ 1.96          /* MAXZ defined by 1.4*1.4 = 1.96 */
  66.  
  67. void Demo(void)
  68. {
  69.   struct DotPixel *object;
  70.   WORD   i;
  71.   WORD   offsetx = (screen->Width/2);
  72.   WORD   offsety = (screen->Height/2);
  73.   double temp;
  74.   double angle=0;
  75.   LONG  colour;
  76.   double Z2,X2,Y2;
  77.   LONG   scale=32;
  78.   UWORD  anglex=0,angley=0,anglez=0;
  79.   double u,v;
  80.   double *sine;       /* Pointer to our sine table */
  81.   double *cosine;     /* Pointer to our cosine table */
  82.  
  83.   object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_DATA);
  84.   sine   = AllocMemBlock(sizeof(double)*360,MEM_DATA);
  85.   cosine = AllocMemBlock(sizeof(double)*360,MEM_DATA);
  86.  
  87.   /* First calculate the X, Y and Z coordinates of our object. */
  88.  
  89.   for (i=0; i<AMTDOTS; i++) {
  90.     u = ((double)FastRandom(28000-1)+1)/10000-1.4;  /*  -1.4 < u < 1.4  */
  91.     v = ((double)FastRandom(28000-1)+1)/10000-1.4;  /*  -1.4 < v < 1.4  */
  92.     object[i].X  = u*(1+v*v)-u*u*u;
  93.     object[i].Y  = v*(1+u*u)-v*v*v;
  94.     object[i].Z  = u*u-v*v;
  95.   }
  96.  
  97.   /* Now generate our cosine and sinus tables */
  98.  
  99.   for (i=0; i<360; i++) {
  100.     cosine[i] = cos(angle);
  101.     sine[i]   = sin(angle);
  102.     angle    += 0.25;
  103.   }
  104.  
  105.   /* Go into our main loop */
  106.  
  107.   do
  108.   {
  109.     Query(jport1);
  110.     scale += jport1->YChange;
  111.     if (scale < 1)   scale = 1;
  112.     if (scale > 100) scale = 100;
  113.  
  114.     Clear(screen->Bitmap);
  115.  
  116.     for (i=0; i<AMTDOTS; i++) {
  117.  
  118.       X2 = object[i].X;
  119.       Y2 = object[i].Y;
  120.       Z2 = object[i].Z;
  121.  
  122.       /* Rotate the X axis */
  123.  
  124.       temp = Z2;
  125.       Z2 = Z2*cosine[anglex] - Y2*sine[anglex];
  126.       Y2 = Y2*cosine[anglex] + temp*sine[anglex];
  127.  
  128.       /* Rotate the Y axis */
  129.  
  130.       temp = Z2;
  131.       Z2 = Z2*cosine[angley] - X2*sine[angley];
  132.       X2 = X2*cosine[angley] + temp*sine[angley];
  133.  
  134.       /* Rotate the Z axis */
  135.  
  136. //      temp = X2;
  137. //      X2 = X2*cosine[anglez] - Y2*sine[anglez];
  138. //      Y2 = Y2*cosine[anglez] + temp*sine[anglez];
  139.  
  140.       /* Calculate colour based on Z position (-1.96 < Z < +1.96) */
  141.  
  142.       colour = (((Z2+MAXZ)/MAXZ)*screen->Bitmap->AmtColours)/2;
  143.  
  144.       /* Finally scale the (x,y) coordinates to enlarge or shrink the sphere */
  145.  
  146.       X2 *= scale;
  147.       Y2 *= scale;
  148.  
  149.       screen->Bitmap->DrawPixel(screen->Bitmap,(WORD)X2+offsetx,(WORD)Y2+offsety,colour);
  150.     }
  151.  
  152.     anglex++; if (anglex >= 360) anglex = 0;
  153.     angley++; if (angley >= 360) angley = 0;
  154.     anglez++; if (anglez >= 360) anglez = 0;
  155.  
  156.     WaitAVBL();
  157.     SwapBuffers(screen);
  158.   } while(!(jport1->Buttons & JD_LMB));
  159.  
  160.   FreeMemBlock(object);
  161.   FreeMemBlock(sine);
  162.   FreeMemBlock(cosine);
  163. }
  164.  
  165.